import type { Plugin } from "vue";

/**
 * Check the integrity of the url
 * @param url isURL
 */
export function isURL(url: string): boolean {
  return /^((ht|f)tps?):\/\/?/.test(url);
}

type IDefinePlugin<T> = T & Plugin;

/**
 * add install function for plugin
 * @param plugin the vue components and plugin
 */
export function withInstall<T extends { name: string }>(
  plugin: T
): IDefinePlugin<T> {
  const _plugin = plugin as IDefinePlugin<T>;

  _plugin.install = (app) => {
    app.component(_plugin.name, _plugin);
  };

  return _plugin;
}
